package com.mox.enterprise.util;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;

import java.util.Map;

/**
 * @author mox
 * @time 2020/2/18 15:53
 */

@Slf4j
public class RedisOpera {

    /**
     * 向缓存中设置字符串内容
     * 失败返回0  不覆盖 成功 返回1
     *
     * @param key   key
     * @param value value
     * @return .
     * @throws Exception
     */
    public static long setnx(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            if (jedis != null) {
                return jedis.setnx(key, value);
            }
        } catch (Exception e) {
            log.error("setnx出错:{}", e.getMessage());
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return 0;
    }

    /**
     * 成功返回  OK
     * 向缓存中设置对象(自动把对象转换成json数据存储到缓层中)
     *
     * @param key
     * @param value
     * @return
     */
    public static long setnx(String key, Object value) {
        Jedis jedis = null;
        try {
            Gson gson = new Gson();
            String objectJson = gson.toJson(value);
            jedis = JedisUtil.getJedis();
            if (jedis != null) {
                return jedis.setnx(key, objectJson);
            }
        } catch (Exception e) {
            log.error("setnx出错:{}", e.getMessage());
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return 0;
    }

    /**
     * 删除缓存中得对象，根据key
     *
     * @param key
     * @return
     */
    public static boolean del(String key) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            if (jedis != null) {
                jedis.del(key);
                return true;
            }
        } catch (Exception e) {
            log.error("数据删除失败:{}", e.getMessage());
            return false;
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return true;
    }


    /**
     * 根据key 获取内容
     *
     * @param key
     * @return
     */
    public static Object get(String key) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            Object value = null;
            if (jedis != null) {
                value = jedis.get(key);
            }
            return value;
        } catch (Exception e) {
            log.error("get出错:{}", e.getMessage());
            return false;
        } finally {
            JedisUtil.returnResource(jedis);
        }

    }


    /**
     * 根据key 获取对象
     *
     * @param key
     * @return
     */
    public static <T> T get(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            if (jedis != null) {
                Gson gson = new Gson();
                String value = jedis.get(key);
                return gson.fromJson(value, clazz);
            }
        } catch (Exception e) {
            log.error("数据获取失败:{}", e.getMessage());
            return null;
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return null;
    }


    /***
     * 检查key是否存在
     * @param key
     * @return
     * true 存在
     * false 不存在
     */
    public static boolean checkExists(String key) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            if (jedis != null) {
                return jedis.exists(key);
            }
        } catch (Exception e) {
            log.error("key值判断失败:{}", e.getMessage());
            return false;
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return true;
    }


    /***
     * 往指定的key追加内容，key不在则添加key
     * @param key
     * @param value
     * @return
     */
    public static boolean appendStr(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            if (jedis != null) {
                jedis.append(key, value);
            }
            return true;
        } catch (Exception e) {
            log.error("内容追加失败:{}", e.getMessage());
            return false;
        } finally {
            JedisUtil.returnResource(jedis);
        }
    }

    /***
     * 批量获取key的value值
     * @param keys
     * @return
     */
    public static Object bathKey(String[] keys) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            if (jedis != null) {
                return jedis.mget(keys);
            }
        } catch (Exception e) {
            log.error("批量获取数据出错:{}", e.getMessage());
            return null;
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return null;
    }

    /***************************************hashes(哈希)类型*********************************************************/

    /**
     * 设置hash field
     * 如果存在不会设置返回0
     *
     * @param key
     * @param field
     * @param value
     * @return 成功返回1, 失败  0
     */
    public static long hsetnx(String key, String field, String value) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            return jedis.hsetnx(key, field, value);
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            JedisUtil.returnResource(jedis);
        }
        return 0;

    }

    /**
     * hget取值(value)
     *
     * @param key
     * @param field
     * @return
     */
    public static Object hget(String key, String field) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            return jedis.hget(key, field);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JedisUtil.returnResource(jedis);
        }
    }

    /**
     * hmset 批量设置值
     *
     * @param key
     * @param hashmap
     * @return 成功返回OK
     */
    public static String hmset(String key, Map<String, String> hashmap) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            return jedis.hmset(key, hashmap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return null;
    }

    /**
     * hmget 批量取值(value)
     *
     * @param key
     * @param
     * @return
     */
    public static Object hmget(String key, String... fields) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            return jedis.hmget(key, fields);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JedisUtil.returnResource(jedis);
        }
    }

    /**
     * @param key
     * @return 返回所有的key和value
     */
    public static Map<String, String> hgetall(String key) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            return jedis.hgetAll(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JedisUtil.returnResource(jedis);
        }
    }


    /***************************************list(列表)*********************************************************/


    /**
     * lpush 设置值 从头部压入一个元素
     *
     * @param key
     * @param strings
     * @return 成功返回成员的数量  失败返回0
     */
    public static long lpush(String key, String... strings) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            return jedis.lpush(key, strings);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return 0;
    }

    /**
     * list列表取值(lrange)
     *
     * @param key
     * @param start
     * @param end
     * @return start=0  end=-1(代表从开始到结束)
     */
    public static Object lrange(String key, long start, long end) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            return jedis.lrange(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return 0;
    }


    public static String rpoplpush(String key, String dstkey) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            return jedis.rpoplpush(key, dstkey);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return null;
    }

    public static String rpop(String key) {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            return jedis.rpop(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JedisUtil.returnResource(jedis);
        }
        return null;
    }

}
